var orgDialog;


$(function() {
	//初始化组织datagrid
	$("#org_table").datagrid({
		url: currenthost + '/admin/query.ajax',
		title: '文档信息',
		pagination: true,
		fitColumns:true,
		pageSize: 10,
		pageList: [10, 20, 30],
		columns: [[{
			field: 'id',
			checkbox: true
		},
		{
			field: 'title',
			title: '公文标题',
			width: 80
		},
		{
			field: 'url',
			title: '稿纸地址',
			width: 100
		},
		{
			field: 'dbPath',
			title: '索引库地址',
			width: 100
		},
		{
			field: 'server',
			title: '服务器',
			width: 50
		},
		{
			field: 'statusDescrib',
			title: '状态',
			width: 50
		},
		{
			field: 'docount',
		    title: '次数',
		    width: 30
		},
		{
			field: 'errorMsg',
			title: '错误描述',
			width: 70
		},
		{
			field: 'gmtModified',
			title: '最近操作时间',
			width: 100
		},
		{
			field: 'logGmtModified',
			hidden: true
		},
		{
			field: 'gmtCreated',
			hidden: true
		},
		{
			field: 'packageName',
			hidden: true
		},
		{
			field: 'type',
			hidden: true
		},
		{
			field: 'docId',
			hidden: true
		},
		{
			field: 'recordId',
			hidden: true
		},
		{
			field: 'attach',
			hidden: true
		},
		{
			field: 'status',
			hidden: true
		}]],
		toolbar: [{
			text: '详细',
			iconCls: 'icon-edit',
			handler: function() {
				admin_org_editFun();
			}
		},
		'-', {
			text: '重新归档',
			iconCls: 'icon-edit',
			handler: function() {
				admin_fill();
			}
		}]
	});

	orgDialog = $('#orgDialog').show().dialog({
		modal: true,
		title: '公文详细',
		width: 350,
		height: 380
	}).dialog('close');


})

function admin_org_editFun() {
	var orgForm = $('#orgForm').form();
	var rows = $("#org_table").datagrid('getSelections');
	if (rows.length != 1 && rows.length != 0) {
		jQuery.messager.show({
			msg: '只能择一份公文！您已经选择了' + rows.length + '份公文',
			title: '提示'
		});
	} else if (rows.length == 1) {
		orgForm.find('[name=id]').attr('readonly', 'readonly');
		orgDialog.dialog('open');
		orgForm.form('clear');
		orgForm.form('load', {
			id: rows[0].id,
			title: rows[0].title,
			url: rows[0].url,
			docount: rows[0].docount,
			server: rows[0].server,
			dbPath: rows[0].dbPath,
			recordId: rows[0].recordId,
			packageName:rows[0].packageName,
			type: rows[0].type,
		    gmtModified:rows[0].gmtModified,
		    gmtCreated:rows[0].gmtCreated,
		    docId:rows[0].docId,
		    logGmtModified:rows[0].logGmtModified,
		    errorMsg:rows[0].errorMsg
		});
	} else if (rows.length == 0) {
		jQuery.messager.show({
			msg: '请选择一份公文！',
			title: '提示'
		});
	}
}

function admin_fill() {
	var rows = $("#org_table").datagrid('getChecked');
	if (rows.length != 1 && rows.length != 0) {
		jQuery.messager.show({
			msg: '只能择一份公文！您已经选择了' + rows.length + '份公文',
			title: '提示'
		});
	} else if (rows.length == 1) {
		if (rows[0].status!='7') {
			jQuery.messager.show({
				msg: '请选择归档失败的且归档次数等于10的文档',
				title: '提示'
			});
		} else if (rows[0].docount < 10) {
			jQuery.messager.show({
				msg: '仍会重新归档，请等待！',
				title: '提示'
			});
		} else {
			jQuery.messager.confirm('确认', '您是否要重新归档当前公文？',
			function(r) {
				if (r) {
					jQuery.ajax({
						url: currenthost + '/fill.ajax',
						data: {
							id: rows[0].id
						},
						dataType: 'json',
						success: function(result) {
							if (result.success) {
								$('#org_table').datagrid('load');
								$('#org_table').datagrid('uncheckAll').datagrid('unselectAll').datagrid('clearSelections');
							}
							jQuery.messager.show({
								title: '提示',
								msg: result.msg
							});
						}
					});
				}
			});
		}
	} else if (rows.length == 0) {
		jQuery.messager.show({
			msg: '请选择一份公文！',
			title: '提示'
		});
	}
}

//查询
function admin_org_searchFun() {
	$('#org_table').datagrid('load', serializeObject($('#admin_org_searchForm')));
}

//清空条件
function admin_org_cleanFun() {
	$('#admin_org_searchForm').form('clear');
}

var serializeObject = function(form) {
	var o = {};
	jQuery.each(form.serializeArray(),
	function(index) {
		if (o[this['name']]) {
			o[this['name']] = o[this['name']] + "," + this['value'];
		} else {
			o[this['name']] = this['value'];
		}
	});
	return o;
};